package com.alpha.eceasy.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.dto.ShoppingCartDTO;
import com.alpha.eceasy.mall.service.MallShoppingCartService;
import com.alpha.eceasy.exception.BizException;
import com.alpha.eceasy.mall.api.entity.MallCommodity;
import com.alpha.eceasy.mall.api.entity.MallCommoditySpecification;
import com.alpha.eceasy.mall.api.entity.MallShoppingCart;
import com.alpha.eceasy.mall.api.vo.MallShoppingCartVO;
import com.alpha.eceasy.mall.mapper.MallShoppingCartMapper;
import com.alpha.eceasy.mall.util.SecurityUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class MallShoppingCartServiceImpl extends BaseServiceImpl<MallShoppingCartMapper, MallShoppingCart> implements MallShoppingCartService {
    private final MallCommodityServiceImpl commodityService;
    private final MallCommoditySpecificationServiceImpl commoditySpecificationService;

    @Override
    public MallShoppingCartVO saveCat(MallShoppingCart mallShoppingCart) {
        //商品规格
        MallCommoditySpecification specification = commoditySpecificationService.getByGuid(mallShoppingCart.getCommoditySpecificationGuid());
        if (specification == null) {
            throw new BizException("无此规格，请重新添加！");
        }
        //商品字典信息
        MallCommodity commodityDict = commodityService.getByGuid(specification.getCommodityGuid());

        MallShoppingCart cart = this.getOne(Wrappers.<MallShoppingCart>lambdaQuery()
                .eq(MallShoppingCart::getCustomerGuid, SecurityUtils.getUserGuid())
                .eq(MallShoppingCart::getCommoditySpecificationGuid, mallShoppingCart.getCommoditySpecificationGuid()));
        //已有叠加数量
        if (cart == null) {
            cart = new MallShoppingCart();
            BeanUtil.copyProperties(mallShoppingCart, cart);
            cart.setCustomerGuid(SecurityUtils.getUserGuid());
            this.save(cart);
        } else {//唯有添加
            cart.setNumber(cart.getNumber() + mallShoppingCart.getNumber());
            cart.setCustomerGuid(SecurityUtils.getUserGuid());
            this.updateById(cart);
        }
        MallShoppingCartVO mallShoppingCartVO = new MallShoppingCartVO();
        BeanUtil.copyProperties(cart, mallShoppingCartVO);
        mallShoppingCartVO.setSellPrice(specification.getSellPrice());
        mallShoppingCartVO.setCommodityName(commodityDict.getCommodityName());
        mallShoppingCartVO.setCommodityIcon(commodityDict.getIcon());
        return mallShoppingCartVO;
    }

    @Override
    public MallShoppingCartVO updateShoppingCartByGuid(MallShoppingCart mallShoppingCart) {
        //商品规格
        MallCommoditySpecification specification = commoditySpecificationService.getByGuid(mallShoppingCart.getCommoditySpecificationGuid());
        if (specification == null) {
            throw new BizException("无此规格，请重新操作！");
        }
        //商品字典信息
        MallCommodity commodity = commodityService.getByGuid(specification.getCommodityGuid());
        //修改购物车
        this.updateByGuid(mallShoppingCart);
        //获取购物车信息并封装
        MallShoppingCart shoppingCart = this.getById(mallShoppingCart.getId());
        MallShoppingCartVO mallShoppingCartVO = new MallShoppingCartVO();
        BeanUtil.copyProperties(shoppingCart, mallShoppingCartVO);
        mallShoppingCartVO.setSellPrice(specification.getSellPrice());
        mallShoppingCartVO.setCommodityName(commodity.getCommodityName());
        mallShoppingCartVO.setCommodityIcon(commodity.getIcon());
        return mallShoppingCartVO;
    }

    @Override
    public List<MallShoppingCartVO> selectListByCustomerGuid() {
        //查询购物车商品
        List<MallShoppingCart> shoppingCartList = this.list(Wrappers.<MallShoppingCart>lambdaQuery()
                .eq(MallShoppingCart::getCustomerGuid, SecurityUtils.getUserGuid())
                .eq(MallShoppingCart::getDeleted, false));
        if (CollectionUtils.isEmpty(shoppingCartList)) {
            return new ArrayList<>();
        }
        List<String> specificationGuidList = shoppingCartList.stream().map(MallShoppingCart::getCommoditySpecificationGuid).toList();
        List<MallCommoditySpecification> commoditySpecificationList = commoditySpecificationService.listByGuids(specificationGuidList);
        List<String> commodityGuidList = commoditySpecificationList.stream().map(MallCommoditySpecification::getCommodityGuid).toList();
        List<MallCommodity> commodityDictList = commodityService.listByGuids(commodityGuidList);
        //封装商品价格
        return shoppingCartList.stream().map(shoppingCart -> {
            MallShoppingCartVO vo = new MallShoppingCartVO();
            BeanUtil.copyProperties(shoppingCart, vo);
            commoditySpecificationList.stream()
                    .filter(specification -> specification.getGuid().equals(shoppingCart.getCommoditySpecificationGuid()))
                    .findFirst()
                    .ifPresent(commoditySpecification -> {
                                vo.setSellPrice(commoditySpecification.getSellPrice());
                                commodityDictList.stream()
                                        .filter(commodityDict -> commodityDict.getGuid().equals(commoditySpecification.getCommodityGuid()))
                                        .findFirst()
                                        .ifPresent(mallCommodity -> {
                                            vo.setCommodityName(mallCommodity.getCommodityName());
                                            vo.setCommodityIcon(mallCommodity.getIcon());
                                        });
                            }
                    );
            MallCommoditySpecification commoditySpecification = commoditySpecificationService.getByGuid(shoppingCart.getCommoditySpecificationGuid());
            vo.setCommoditySpecification(commoditySpecification);
            vo.setCommodity(commodityService.getByGuid(commoditySpecification.getCommodityGuid()));
            return vo;
        }).toList();
    }

    @Override
    public Boolean removeByCustomerId() {
        return this.remove(Wrappers.<MallShoppingCart>lambdaQuery()
                .eq(MallShoppingCart::getCustomerGuid, SecurityUtils.getUserGuid()));
    }

    @Override
    public Boolean removeByGuidList(ShoppingCartDTO dto) {
        return this.remove(Wrappers.<MallShoppingCart>lambdaQuery()
                .in(Objects.nonNull(dto), MallShoppingCart::getGuid, dto.guidList()));
    }
}
